#!/usr/bin/env python

# Python imports
import unittest
import sys
import inspect

# from openid import *
from amsoftopenid.issoservice import IssoService
from amsoftopenid.constants import *
from amsoftopenid.properties import Properties

# Iservices-specific Exception classes
from amsoftopenid.exception import IservicesException
from amsoftopenid.exception import IservicesSysException

class TestIssoService(unittest.TestCase):
    
    validGinu = "!!10099"
    invalidGinu = "!!10095"
    validIBrokerName = "localhost"
    invalidIBrokerName = "local_inactive"
    validSessionCookie = "8B2e1XawUWTwJLxNmDoz6GYFEN4="
    invalidSessionCookie = "ESCsj1QeHVjCBoNGk043ZVp8FjM="
    validMachineCookie = "xM/J7+zjuY5C+6QIYnbq1oNypSM="
    invalidMachineCookie = "HRtmZmZMfcy6lHZeXgPfVDJXxTc="
    validPassword = "hello"
    invalidPassword = "invalidPassword"
    validGreeting = "tic tac toe"
    invalidGreeting = "invalidGreeting"
    
    def setUp(self):
        pass
    
    """
    #Private method to get DB connection
    def _getDBConn():
        # Get absolute file path for properties file
        sPath = inspect.getfile(sys._getframe(1))
        iLen = sPath.index ('\python') + 7
        sPath = sPath[0:iLen]
        sPath = sPath + '\\amsoftopenid\db.properties'
        
        p = Properties()
        p.load(open(sPath))
        
        try:
            dbpool = DBConnectionPool(p['host'], p['user_name'], p['password'],
            p['database'], p['maxConnection'], 5, p['waitTime']).getTheInstance()
            dbConnection = dbpool.getConnection()
        except:
            raise IservicesException(NO_DATABASE_CONNECTION,
                                    dUtilErrorCodeMap[NO_DATABASE_CONNECTION])
        
        return dbConnection
    """
    
    def tearDown(self):
        pass
    
    def testVerifyPrincipalNoGinu(self):
        print "In testVerifyPrincipalNoGinu()"
        sGinu = ""
        sIBrokerName = self.validIBrokerName
        try:
            IssoService.verifyPrincipal(sGinu, sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")

    def testVerifyPrincipalNoIBrokerName(self):
        print "In testVerifyPrincipalNoIBrokerName()"
        sGinu = self.validGinu
        sIBrokerName = ""
        try:
            IssoService.verifyPrincipal(sGinu, sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifyPrincipalAllInvalid(self):
        print "In testVerifyPrincipalValidGinu()"
        sGinu = self.invalidGinu
        sIBrokerName = self.invalidIBrokerName
        try:
            IssoService.verifyPrincipal(sGinu, sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifyPrincipalInvalidGinu(self):
        print "In testVerifyPrincipalInvalidGinu()"
        sGinu = self.invalidGinu
        sIBrokerName = self.validIBrokerName
        try:
            IssoService.verifyPrincipal(sGinu, sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifyPrincipalInvalidIBrokerName(self):
        print "In testVerifyPrincipalInvalidIBrokerName()"
        sGinu = self.validGinu
        sIBrokerName = self.invalidIBrokerName
        try:
            IssoService.verifyPrincipal(sGinu, sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifyPrincipalAllValid(self):
        print "In testVerifyPrincipalValidGinu()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        self.assert_(IssoService.verifyPrincipal(sGinu, sIBrokerName))
    
    def testGenerateCookieNoGinu(self):
        print "In testGenerateCookieNoGinu()"
        sGinu = None
        try:
            IssoService.generateCookie(sGinu)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testGenerateCookieEmptyGinu(self):
        print "In testGenerateCookieEmptyGinu()"
        sGinu = ''
        try:
            IssoService.generateCookie(sGinu)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testGenerateCookieValidGinu(self):
        print "In testGenerateCookieValidGinu()"
        sGinu = self.validGinu
        self.assert_(IssoService.generateCookie(sGinu))
    
    def testGetIBrokerDataNoIBrokerName(self):
        print "In testGetIBrokerDataNoIBrokerName()"
        sIBrokerName = ""
        try:
            IssoService.getIBrokerData(sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testGetIBrokerDataInvalidIBrokerName(self):
        print "In testGetIBrokerDataInvalidIBrokerName()"
        sIBrokerName = self.invalidIBrokerName
        try:
            IssoService.getIBrokerData(sIBrokerName)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testGetIBrokerDataValidIBrokerName(self):
        print "In testGetIBrokerDataValidIBrokerName()"
        sIBrokerName = self.validIBrokerName
        self.assert_(IssoService.getIBrokerData(sIBrokerName))
    
    def testGetPrincipalMetadataNoGinu(self):
        print "In testGetPrincipalMetadataNoGinu()"
        sGinu = ""
        try:
            IssoService.getPrincipalMetadata(sGinu)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testGetPrincipalMetadataInvalidGinu(self):
        print "In testGetPrincipalMetadataInvalidGinu()"
        sGinu = self.invalidGinu
        try:
            IssoService.getPrincipalMetadata(sGinu)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testGetPrincipalMetadataValidGinu(self):
        print "In testGetPrincipalMetadataValidGinu()"
        sGinu = self.validGinu
        sGreeting = IssoService.getPrincipalMetadata(sGinu)
        self.assert_(sGreeting == self.validGreeting)
    
    def testLoginNoGinu(self):
        print "In testLoginNoGinu()"
        sGinu = ""
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.validMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginNoIBrokerName(self):
        print "In testLoginNoIBrokerName()"
        sGinu = self.validGinu
        sIBrokerName = ""
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.validMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginNoSessionCookie(self):
        print "In testLoginNoSessionCookie()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = ""
        sMachineCookie = self.validMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginNoMachineCookie(self):
        print "In testLoginNoMachineCookie()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = ""
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginNoPassword(self):
        print "In testLoginNoPassword()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.invalidSessionCookie
        sMachineCookie = self.validMachineCookie
        sPassword = ""
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginInvalidGinu(self):
        print "In testLoginInvalidGinu()"
        sGinu = self.invalidGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.validMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginInvalidIBrokerName(self):
        print "In testLoginInvalidIBrokerName()"
        sGinu = self.validGinu
        sIBrokerName = self.invalidIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.validMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginInvalidSessionCookie(self):
        print "In testLoginInvalidSessionCookie()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.invalidSessionCookie
        sMachineCookie = self.validMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2009,
                             "Incorrect exception code. Expected 2009")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown" + str(oEx))
    
    def testLoginInvalidMachineCookie(self):
        print "In testLoginInvalidMachineCookie()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.invalidMachineCookie
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2009,
                             "Incorrect exception code. Expected 2009")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginInvalidPassword(self):
        print "In testLoginInvalidPassword()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.validMachineCookie
        sPassword = self.invalidPassword
        try:
            IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie, sPassword)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 1001,
                             "Incorrect exception code. Expected 1001")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testLoginValidSessionCookie(self):
        print "In testLoginValidSessionCookie()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.validSessionCookie
        sMachineCookie = self.validMachineCookie
        self.assert_(IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie))
    
    def testLoginValidMachineCookie(self):
        print "In testLoginValidMachineCookie()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.invalidSessionCookie
        sMachineCookie = self.validMachineCookie
        self.assert_(IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie))
    
    def testLoginValidPassword(self):
        print "In testLoginValidPassword()"
        sGinu = self.validGinu
        sIBrokerName = self.validIBrokerName
        sSessionCookie = self.invalidSessionCookie
        sMachineCookie = self.validMachineCookie
        sPassword = self.validPassword
        self.assert_(IssoService.login(sGinu, sIBrokerName, sSessionCookie, sMachineCookie, sPassword))
    
    def testLogoutNoCookie(self):
        print "In testLogoutNoCookie()"
        sSessionCookie = None
        self.assert_(IssoService.logout(sSessionCookie))
    
    def testLogoutBlankCookie(self):
        print "In testLogoutBlankCookie()"
        sSessionCookie = ''
        self.assert_(IssoService.logout(sSessionCookie))
    
    def testLogoutInvalidCookie(self):
        print "In testLogoutInvalidCookie()"
        sSessionCookie = self.invalidSessionCookie
        self.assert_(IssoService.logout(sSessionCookie))
    
    def testLogoutValidCookie(self):
        print "In testLogoutValidCookie()"
        sSessionCookie = self.validSessionCookie
        self.assert_(IssoService.logout(sSessionCookie))
    
    def testVerifySessionStateNoGinu(self):
        print "In testVerifySessionStateNoGinu()"
        sGinu = ""
        sCookie = self.validSessionCookie
        bIsSessionCookie = "true"
        try:
            IssoService.verifySessionState(sGinu, sCookie, bIsSessionCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifySessionStateNoSessionCookie(self):
        print "In testVerifySessionStateNoSessionCookie()"
        sGinu = self.validGinu
        sCookie = ""
        bIsSessionCookie = "true"
        try:
            IssoService.verifySessionState(sGinu, sCookie, bIsSessionCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifySessionStateNoIsSessionCookie(self):
        print "In testVerifySessionStateNoIsSessionCookie()"
        sGinu = self.validGinu
        sCookie = self.validSessionCookie
        bIsSessionCookie = ""
        try:
            IssoService.verifySessionState(sGinu, sCookie, bIsSessionCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2028,
                             "Incorrect exception code. Expected 2028")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifySessionStateInvalidGinu(self):
        print "In testVerifySessionStateInvalidGinu()"
        sGinu = self.invalidGinu
        sCookie = self.validSessionCookie
        bIsSessionCookie = "true"
        try:
            IssoService.verifySessionState(sGinu, sCookie, bIsSessionCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2004,
                             "Incorrect exception code. Expected 2004")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifySessionStateInvalidSessionCookie(self):
        print "In testVerifySessionStateInvalidSessionCookie()"
        sGinu = self.validGinu
        sCookie = self.invalidSessionCookie
        bIsSessionCookie = "true"
        try:
            IssoService.verifySessionState(sGinu, sCookie, bIsSessionCookie)
        except IservicesSysException:
            oEx = sys.exc_info()[1]
            self.assertEqual(oEx.getCode(), 2009,
                             "Incorrect exception code. Expected 2009")
        except:
            oEx = sys.exc_info()[1]
            self.assertEqual(IservicesSysException, oEx, "Incorrect exception thrown")
    
    def testVerifySessionStateAllValid(self):
        print "In testVerifySessionStateAllValid()"
        sGinu = self.validGinu
        sCookie = 'hello'
        bIsSessionCookie = True
        self.assert_(IssoService.verifySessionState(sGinu, sCookie, bIsSessionCookie))

def main():
    unittest.main()

if __name__ == '__main__':
    main()
